<?php

namespace App\Jobs;

use EasyWeChat\Factory;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class GoClass implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $userInfo;
    protected $courseInfo;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userInfo, $courseInfo)
    {
        $this->userInfo   = $userInfo;
        $this->courseInfo = $courseInfo;
    }

    /**
     * @return bool
     */
    public function handle()
    {
        //服务通知加一个
        $app      = Factory::miniProgram(config('wechat.mini_program.default'));
        $toUser   = $this->userInfo['openid'];
        $sendData = [
            'touser'      => $toUser,
            'template_id' => 'D8hMY2d_UnZRBVArdUZzgMI2Bhfh1Qx2tqcBWfufmU8',
            'data'        => [
                'thing1' => $this->courseInfo['name'],
                'date2'  => $this->courseInfo['start_time'],
                'name3'  => $this->userInfo['nickname'],
                'thing4' => '预约成功',
                'thing5' => '预约结果通知'
            ],
            'page'        => 'pages/campus/index'
        ];

        //发送给用户对应的关联者
        $rel = $app->subscribe_message->send($sendData);
        Log::info('服务通知结果', ['rel' => json_encode($rel),'send_data'=>json_encode($sendData['data'])]);

        return true;
    }
}
